package com.wst.system.server.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.system.config.exception.GlobalExcetion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.util.JSONUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.server.entity.SysOpenApi;
import com.wst.system.server.mapper.SysOpenApiMapper;
import com.wst.system.server.service.SysOpenApiService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import com.wst.system.server.entity.vo.SysOpenApiVo;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2025-01-16
 */
@Service
@Transactional
public class SysOpenApiServiceImpl extends ServiceImpl<SysOpenApiMapper, SysOpenApi> implements SysOpenApiService {


    @Override
    public SysOpenApi getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SysOpenApi sysopenapi) {
        return super.updateById(sysopenapi);
    }


    public SysOpenApi saveSysOpenApi(SysOpenApi sysopenapi) throws GlobalExcetion {
        super.save(sysopenapi);
        return sysopenapi;
    }


    public SysOpenApi updateSysOpenApiById(SysOpenApi sysopenapi) throws GlobalExcetion{
        super.updateById(sysopenapi);
        return sysopenapi;
    }


    public SysOpenApi saveOrUpdateSysOpenApi(SysOpenApiVo sysopenapiVo) throws GlobalExcetion{
        SysOpenApi sysopenapi=new SysOpenApi();
        BeanUtils.copyProperties(sysopenapiVo,sysopenapi);
        if(sysopenapiVo.getId()!=null){
            SysOpenApi sysopenapiEntity=super.baseMapper.selectById(sysopenapiVo.getId());
            if(sysopenapiEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            super.baseMapper.updateById(sysopenapi);
        }else{
            super.baseMapper.insert(sysopenapi);
        }
        return sysopenapi;
    }

    public IPage<SysOpenApi> page(PageSearchVo vo) throws GlobalExcetion{
        LambdaQueryWrapper<SysOpenApi> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(JSONUtil.isNotBlank(vo.getSearch(),"apiName")){
            lambdaQueryWrapper.like(SysOpenApi::getApiName,vo.getString("apiName"));
        }
        if(JSONUtil.isNotBlank(vo.getSearch(),"apiUrl")){
            lambdaQueryWrapper.like(SysOpenApi::getApiUrl,vo.getString("apiUrl"));
        }
        lambdaQueryWrapper.orderByDesc(SysOpenApi::getCreateTime);
        IPage<SysOpenApi> iPage=super.baseMapper.selectPage(new Page(vo.getPageNum(),vo.getPageSize()),lambdaQueryWrapper);
        return iPage;
    }

    @Override
    public List<SysOpenApi> getSysOpenApiListByApiIdList(List<String> ids) throws GlobalExcetion {
        LambdaQueryWrapper<SysOpenApi> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysOpenApi::getId, ids);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }
}